//保留两位小数
export const XtoFixed = (price) => {
  // return Math.round(price * 100) / 100;
  let value: any = Math.round(parseFloat(price) * 100) / 100;
  let xsd: any = value.toString().split(".");
  if (xsd.length == 1) {
    value = value.toString() + ".00";
    return value;
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + "0";
    }
    return value;
  }
}

/**
 * OSS图片域名前缀
 */
export const imagesOssPrefix = (url, zoomScale) => {
  // TODO: 上线更换配置
	let ossPrefixDomain = 'http://hongsen-jx-zhjd.oss-cn-hangzhou.aliyuncs.com/'
	let style = ''
	if(zoomScale){
		style = '?x-oss-process=image/resize,p_' + zoomScale
	}
	// let style = '?x-oss-process=image/resize,p_' + '80'
	// if (url.indexOf('http://') != -1 || url.indexOf('https://') != -1){
	// 	return url + style
	// }
	let separator = ''
	if(url.substring(0,1) !== '/'){
		separator = '/'
	}
	return ossPrefixDomain  + separator + url + style
}

/**
 * 产生随机整数，包含下限值，包括上限值
 * @param {Number} lower 下限
 * @param {Number} upper 上限
 * @return {Number} 返回在下限到上限之间的一个随机整数
 */
export const xRandom = (lower: number, upper: number) => {
  return Math.floor(Math.random() * (upper - lower)) + lower
}

/**
 * 获取指定数组的属性值是否存在，存在返回数组坐标，不存在return false
 * @param arr 数组
 * @param attr 属性
 * @param value 属性值
 */
export const xIsArrAttr = (arr, attr, value) => {
  var out = arr.findIndex((item) => {
    if (item[attr] == value) {
      return true
    }
  })
  if (out == -1) {
    return false
  }
  return out
}

/**
 * 检查对象是否存在方法
 */
export const xCheckObj = (param) => {
  if (typeof param == 'undefined' || JSON.stringify(param) == '{}') {
    return false
  }
  return true
}

/**
 * 数组元素交换位置
 * @param arr
 * @param index1
 * @param index2
 */
export const xSwapArray = (arr, index1, index2) => {
  arr[index1] = arr.splice(index2, 1, arr[index1])[0]
  return arr
}

/**
 * 过滤前后空格
 */
export const filterTrim = (str: string) => {
  if (!str) {
    return str
  }
  return str.replace(/(^\s*)|(\s*$)/g, '')
}

/**
 * 过滤所有空格
 */
export const filterAllSpace = (str: string) => {
  if (!str) {
    return str
  }
  return String(str).replace(/\s/g, '')
}

/**
 * 过滤表情
 */
export const filterExpress = (str) => {
  if (!str) {
    return str
  }
  return String(str).replace(
    /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g,
    ''
  )
}

/**
 * 获取url的参数，可以解决乱码问题
 */
export const getRequest = function (name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  var SEARCH = window.location.href.split('?')[1]
    ? window.location.href.split('?')[1]
    : ''
  var r = SEARCH.match(reg)
  if (r != null) {
    //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
    return decodeURI(r[2])
  }
  return null
}

/**
 *日期时间不够两位自动补0
 */
export const getZero = function (num: number) {
  if (num < 10) {
    return '0' + num
  }
  return num
}

/**
 *转化时间2019-01-28T07:39:00.000Z
 *转化成xxxx-xx-xx xx:xx:xx
 */
export const timeToYMdHms = (str) => {
  let d = new Date(str)
  return (
    d.getFullYear() +
    '-' +
    getZero(d.getMonth() + 1) +
    '-' +
    getZero(d.getDate()) +
    ' ' +
    getZero(d.getHours()) +
    ':' +
    getZero(d.getMinutes()) +
    ':' +
    getZero(d.getSeconds())
  )
}
/**
 *转化时间2019-01-28T07:39:00.000Z 转化成xxxx-xx-xx xx:xx
 */
export const timeToYMdHm = (str) => {
  let d = new Date(str)
  return (
    d.getFullYear() +
    '-' +
    getZero(d.getMonth() + 1) +
    '-' +
    getZero(d.getDate()) +
    ' ' +
    getZero(d.getHours()) +
    ':' +
    getZero(d.getMinutes())
  )
}

/**
 *转化时间2019-01-28T07:39:00.000Z 转化成xxxx-xx-xx
 */
export const timeToYMd = (str) => {
  let d = new Date(str)
  return (
    d.getFullYear() +
    '-' +
    getZero(d.getMonth() + 1) +
    '-' +
    getZero(d.getDate())
  )
}

/**
 *转化时间2019-01-28T07:39:00.000Z 转化成xxxx年xx月xx日
 */
export const timeToYMdC = (str) => {
  let d = new Date(str)
  return (
    d.getFullYear() +
    '年' +
    getZero(d.getMonth() + 1) +
    '月' +
    getZero(d.getDate()) +
    '日'
  )
}

/**
 *时间转化格式   xx月xx日 xx:xx
 */
export const timeToMdHmChinese = (str) => {
  if (str) {
    let d = new Date(str)
    return (
      getZero(d.getMonth() + 1) +
      '月' +
      getZero(d.getDate()) +
      '日 ' +
      getZero(d.getHours()) +
      ':' +
      getZero(d.getMinutes())
    )
  }
  return ''
}

/**
 *时间转换格式 xx:xx
 */
export const timeToHm = (str) => {
  if (str) {
    let d = new Date(str)
    return getZero(d.getHours()) + ':' + getZero(d.getMinutes())
  }
  return ''
}

/**
 *两个时间相减得分钟 time2-time1
 */
export const reduceTimeToMinute = (time1, time2) => {
  if (time1 && time2) {
    let timestamp1 = new Date(time1).getTime()
    let timestamp2 = new Date(time2).getTime()
    return Math.round((timestamp2 - timestamp1) / 1000 / 60)
  }
  return ''
}

/**
 *两个时间相减得天数（向上取整） time2-time1
 */
export const reduceTimeToDay = (time1, time2) => {
  if (time1 && time2) {
    let timestamp1 = new Date(time1).getTime()
    let timestamp2 = new Date(time2).getTime()
    return Math.ceil((timestamp2 - timestamp1) / 1000 / 60 / 60 / 24)
  }
  return ''
}

/**
 *手机号码脱敏方法
 */
export const xDataDesensitization = (num) => {
  var len = num.length
  if (len && len > 4) {
    return num.substring(0, 3) + '****' + num.substring(len - 4, len)
  }
}

/**
   *解决关闭对话框时，body的style样式还存留的问题
   */
export const clearBodyStyle = () => {
  let vm: any = window
  vm.document.body.style = ''
}

/**
   *名字脱敏方法
   */
export const xNameDesensitization = (name) => {
  let newStr
  let length = name.length
  if (length === 2) {
    newStr = name.substr(0, 1) + '*'
  }
  if (length > 2) {
    let char = ''
    for (let i = 0, len = length - 2; i < len; i++) {
      char += '*'
    }
    newStr = name.substr(0, 1) + char + name.substr(-1, 1)
  }
  return newStr
}